/*
 * @Description: 全宗上下文
 * @Author: Rfan
 * @Date: 2022-01-08 16:30:05
 * @LastEditTime: 2022-02-17 11:24:22
 */

import { createContext, useContext, useEffect, useReducer } from 'react';
import {
  ADD_LEVEL_BY_ID,
  ADD_LEVEL_METADATA,
  ARC_TYPE_LOADING,
  CLEAR_LEVEL_MATEDATA,
  EDIT_LEVEL_LIST,
  METADATA_LOADING,
  METADATA_LOADING_FINISH,
  SET_ARC_TYPE_LIST,
  SET_CURRENT_METADATA,
  SET_DIALOG_STATE,
  SET_FONDS_LIST,
  SET_METADATA_LIST
} from '../constants';
import { ArchivingReducer } from '../reducers/ArchivingReducer';
import { fetchTableField } from '@/api/system';
import { fetchATListByFondId, fetchLevelByATId } from '@/api/archivesType';
import { fetchFondsListByUnitId } from '@/api/fonds';

const initialState = {
  fondsList: [],
  ATList: [],
  metadataLoading: false,
  // 当前档案类型所有级别元数据列表
  levelMatedata: {},
  // 当前级别元数据列表
  metadataList: [],
  levelList: [],
  currentFond: {},
  // 当前档案类型
  currentAt: 0,
  // 当前级别
  currentLevel: '1',

  // 添加项目弹窗
  arrFormVisible: false
};

const ArchivingContext = createContext();

const ArchivingContextProvider = (props) => {
  const [state, dispatch] = useReducer(ArchivingReducer, initialState);

  const actions = {
    fetchFondsList() {
      fetchFondsListByUnitId(2).then((res) => {
        const { data } = res;

        console.log('fondsList', data);
        dispatch({ type: SET_FONDS_LIST, data });
      });
    },

    async fetchArcList(id) {
      dispatch({ type: ARC_TYPE_LOADING });
      const res = await fetchATListByFondId(id);
      const { data } = res;
      dispatch({ type: SET_ARC_TYPE_LIST, data, id });
    },

    async fetchATLevel(id) {
      const res = await fetchLevelByATId(id);
      const { data } = res;
      dispatch({ type: ADD_LEVEL_BY_ID, data, id });
    },

    async fetchMateData(tableName) {
      dispatch({ type: METADATA_LOADING });
      fetchTableField(tableName)
        .then((res) => {
          const { data } = res;
          dispatch({ type: SET_METADATA_LIST, data });
        })
        .catch((err) => {
          console.log(err);
          dispatch({ type: SET_METADATA_LIST, data: [] });
        })
        .finally(() => {
          dispatch({ type: METADATA_LOADING_FINISH });
        });
    },
    setLevelMatedata(level, data) {
      dispatch({ type: ADD_LEVEL_METADATA, level, data });
    },
    setCurrentData(level) {
      dispatch({ type: SET_CURRENT_METADATA, level });
    },
    clearLevelMatedata() {
      dispatch({ type: CLEAR_LEVEL_MATEDATA });
    },

    // 查询下级
    fetchDownloadData(query) {
      // fetchDownLevelList(query)
      //   .then(res => {
      //     console.log('context down level', res.data);
      const { level } = query.param;
      // const id = query.param.downLevelId;
      //     const data = res.data.results || [];
      //     dispatch({ type: ADD_LEVEL_METADATA, level, data })ss;
      //     dispatch({ type: SET_CURRENT_METADATA, level });
      //   });
      dispatch({ type: EDIT_LEVEL_LIST, level, data: query.param });
    },

    // ---------- 添加项目弹窗 ----------------------------------------------------
    /**
     * 打开添加项目弹窗
     */
    showArrFormDialog() {
      dispatch({ type: SET_DIALOG_STATE, state: true });
    },

    /**
     * 关闭添加项目弹窗
     */
    hideArrFormDialog() {
      dispatch({ type: SET_DIALOG_STATE, state: false });
    }
  };

  useEffect(() => {
    actions.fetchFondsList();
  }, []);

  return (
    <ArchivingContext.Provider value={{ ...state, ...actions }}>
      {props.children}
    </ArchivingContext.Provider>
  );
};

export const useArchivingContext = () => {
  return useContext(ArchivingContext);
};

export { ArchivingContext, ArchivingContextProvider };
